//
//  WmcSliderAlertView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/11.
//

import UIKit
import YPBedrock

class WmcSliderAlertView: WmcSelectView {
    // Lifecycle

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    /// 实例化
    /// - Parameters:
    ///   - values: 值组
    ///   - isScrollToStep: 是否滑动校准
    convenience init(values: [WmcSliderView.Model],
                     isScrollToStep: Bool = true)
    {
        self.init(frame: .zero)
        let slider = WmcSliderView(values: values,
                                   isScrollToStep: isScrollToStep)
        self.slider = slider
        _makeUI()
        _bindViewAction()
    }

    // Internal

    var progressChangeBlock: YPBlock.OneParam<CGFloat>?

    lazy var progressLabel: UILabel = {
        let label = UILabel.YP.Create(font: 16.yp.uiFont(weight: .bold),
                                      color: UIColor.wmc_mainTheme,
                                      alignment: .center)
        return label
    }()

    var slider: WmcSliderView!

    lazy var submitBtn: UIButton = {
        let btn = UIButton.Wmc_themeColorBtn(color: .wmc_mainTheme, title: "确定")
        return btn
    }()

    func _makeUI() {
        super.makeUI()

        titleView.leftBtn.setImage("close".wmc_image, for: .normal)

        addSubview(progressLabel)
        progressLabel.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp_bottomMargin).offset(Wmc_standardEdge.top * 2)
            make.left.right.equalToSuperview()
            make.height.equalTo(18)
        }
        
        addSubview(slider)
        slider.snp.makeConstraints { make in
            make.top.equalTo(progressLabel.snp.bottom).offset(16)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(50)
        }

        addSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.top.equalTo(slider.snp.bottom).offset(48)
            make.left.equalTo(50)
            make.right.equalTo(-50)
            make.height.equalTo(44)
        }
        
        setProgressLabel(0)
    }

    func _bindViewAction() {
        slider.valueChangeBlock = { [weak self] value in
            guard let weakSelf = self else { return }
            weakSelf.setProgressLabel(value)
            weakSelf.progressChangeBlock?(value)
        }
    }
    
    func setProgressLabel(_ progress: CGFloat ) {
        progressLabel.text = "\(Int(progress * 100))%"
    }

    /// 设置滑向
    func setSliderModel(_ model: WmcSliderView.Model) {
        slider.progress = model.progress
        setProgressLabel(model.progress)
    }

    /// 设置滑向
    func setProgress(_ progress: CGFloat) {
        slider.progress = progress
        setProgressLabel(progress)
    }
}
